home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / KoUnitWidgets.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-05-30  |  7.4 KB  |  247 lines

  1. /* This file is part of the KDE project
  2.    Copyright (C) 2002, Rob Buis(buis@kde.org)
  3.    Copyright (C) 2004, Nicolas GOUTTE <goutte@kde.org>
  4.  
  5.    This library is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public
  7.    License as published by the Free Software Foundation; either
  8.    version 2 of the License, or (at your option) any later version.
  9.  
  10.    This library is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    Library General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU Library General Public License
  16.    along with this library; see the file COPYING.LIB.  If not, write to
  17.    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18.  * Boston, MA 02110-1301, USA.
  19. */
  20.  
  21. #ifndef __KOUNITWIDGETS_H__
  22. #define __KOUNITWIDGETS_H__
  23.  
  24. #include <knuminput.h>
  25. #include <knumvalidator.h>
  26. #include <klineedit.h>
  27. #include <kcombobox.h>
  28. #include <KoUnit.h>
  29. #include <koffice_export.h>
  30.  
  31.  
  32. // ----------------------------------------------------------------
  33. //                          Support classes
  34.  
  35.  
  36. class KoUnitDoubleBase;
  37.  
  38. // ### TODO: put it out of the public header file (if possible)
  39. /**
  40.  * Validator for the unit widget classes
  41.  * \internal
  42.  * \since 1.4 (change of behavior)
  43.  */
  44. class KOFFICEUI_EXPORT KoUnitDoubleValidator : public KDoubleValidator
  45. {
  46. public:
  47.     KoUnitDoubleValidator( KoUnitDoubleBase *base, QObject *parent, const char *name = 0 );
  48.  
  49.     virtual QValidator::State validate( QString &, int & ) const;
  50.  
  51. private:
  52.     KoUnitDoubleBase *m_base;
  53. };
  54.  
  55.  
  56. /**
  57.  * Base for the unit widgets
  58.  * \since 1.4 (change of behavior)
  59.  */
  60. class KOFFICEUI_EXPORT KoUnitDoubleBase
  61. {
  62. public:
  63.     KoUnitDoubleBase( KoUnit::Unit unit, unsigned int precision ) : m_unit( unit ), m_precision( precision ) {}
  64.     virtual ~KoUnitDoubleBase() {}
  65.  
  66.     virtual void changeValue( double ) = 0;
  67.     virtual void setUnit( KoUnit::Unit = KoUnit::U_PT ) = 0;
  68.  
  69.     void setValueInUnit( double value, KoUnit::Unit unit )
  70.     {
  71.         changeValue( KoUnit::ptToUnit( KoUnit::fromUserValue( value, unit ), m_unit ) );
  72.     }
  73.  
  74.     void setPrecision( unsigned int precision ) { m_precision = precision; };
  75.  
  76. protected:
  77.     friend class KoUnitDoubleValidator;
  78.     /**
  79.      * Transform the double in a nice text, using locale symbols
  80.      * @param value the number as double
  81.      * @return the resulting string
  82.      */
  83.     QString getVisibleText( double value ) const;
  84.     /**
  85.      * Transfrom a string into a double, while taking care of locale specific symbols.
  86.      * @param str the string to transform into a number
  87.      * @param ok true, if the conversion was succesful
  88.      * @return the value as double
  89.      */
  90.     double toDouble( const QString& str, bool* ok ) const;
  91.  
  92. protected:
  93.     KoUnitDoubleValidator *m_validator;
  94.     KoUnit::Unit m_unit;
  95.     unsigned int m_precision;
  96. };
  97.  
  98.  
  99. // ----------------------------------------------------------------
  100. //                          Widget classes
  101.  
  102.  
  103. /**
  104.  * Spin box for double precision numbers with unit display
  105.  * \since 1.4 (change of behavior)
  106.  */
  107. class KOFFICEUI_EXPORT KoUnitDoubleSpinBox : public KDoubleSpinBox, public KoUnitDoubleBase
  108. {
  109.     Q_OBJECT
  110. public:
  111.     KoUnitDoubleSpinBox( QWidget *parent = 0L, const char *name = 0L );
  112.     // lower, upper, step and value are in pt
  113.     KoUnitDoubleSpinBox( QWidget *parent, double lower, double upper, double step, double value = 0.0,
  114.                          KoUnit::Unit unit = KoUnit::U_PT, unsigned int precision = 2, const char *name = 0 );
  115.     // added so the class can be used in .ui files(by Tymoteusz Majewski, maju7@o2.pl)
  116.     virtual void changeValue( double );
  117.     virtual void setUnit( KoUnit::Unit = KoUnit::U_PT );
  118.  
  119.     /// @return the current value, converted in points
  120.     double value( void ) const;
  121.  
  122.     /// Set minimum value in points.
  123.     void setMinValue(double min);
  124.  
  125.     /// Set maximum value in points.
  126.     void setMaxValue(double max);
  127.  
  128.     /// Set step size in the current unit.
  129.     void setLineStep(double step);
  130.  
  131.     /// Set step size in points.
  132.     void setLineStepPt(double step);
  133.  
  134.     /// Set minimum, maximum value and the step size (all in points) (by Tymoteusz Majewski, maju7@o2.pl)
  135.     void setMinMaxStep( double min, double max, double step );
  136.  
  137. signals:
  138.     /// emitted like valueChanged in the parent, but this one emits the point value
  139.     void valueChangedPt( double );
  140.  
  141.  
  142. private:
  143.     double m_lowerInPoints; ///< lowest value in points
  144.     double m_upperInPoints; ///< highest value in points
  145.     double m_stepInPoints;  ///< step in points
  146.  
  147. private slots:
  148.     // exists to do emits for valueChangedPt
  149.     void privateValueChanged();
  150. };
  151.  
  152.  
  153. /**
  154.  * Line edit for double precision numbers with unit display
  155.  * \since 1.4 (change of behavior)
  156.  */
  157. class KOFFICEUI_EXPORT KoUnitDoubleLineEdit : public KLineEdit, public KoUnitDoubleBase
  158. {
  159.     Q_OBJECT
  160. public:
  161.     KoUnitDoubleLineEdit( QWidget *parent = 0L, const char *name = 0L );
  162.     KoUnitDoubleLineEdit( QWidget *parent, double lower, double upper, double value = 0.0, KoUnit::Unit unit = KoUnit::U_PT, unsigned int precision = 2, const char *name = 0 );
  163.  
  164.     virtual void changeValue( double );
  165.     virtual void setUnit( KoUnit::Unit = KoUnit::U_PT );
  166.  
  167.     /// @return the current value, converted in points
  168.     double value( void ) const;
  169.  
  170. protected:
  171.     bool eventFilter( QObject* obj, QEvent* ev );
  172.  
  173. private:
  174.     double m_value;
  175.     double m_lower;
  176.     double m_upper;
  177.     double m_lowerInPoints; ///< lowest value in points
  178.     double m_upperInPoints; ///< highest value in points
  179. };
  180.  
  181. /**
  182.  * Combo box for double precision numbers with unit display
  183.  * \since 1.4 (change of behavior)
  184.  */
  185. class KOFFICEUI_EXPORT KoUnitDoubleComboBox : public KComboBox, public KoUnitDoubleBase
  186. {
  187.     Q_OBJECT
  188. public:
  189.     KoUnitDoubleComboBox( QWidget *parent = 0L, const char *name = 0L );
  190.     KoUnitDoubleComboBox( QWidget *parent, double lower, double upper, double value = 0.0, KoUnit::Unit unit = KoUnit::U_PT, unsigned int precision = 2, const char *name = 0 );
  191.  
  192.     virtual void changeValue( double );
  193.     void updateValue( double );
  194.     virtual void setUnit( KoUnit::Unit = KoUnit::U_PT );
  195.  
  196.     /// @return the current value, converted in points
  197.     double value( void ) const;
  198.     void insertItem( double, int index = -1 );
  199.  
  200. protected:
  201.     bool eventFilter( QObject* obj, QEvent* ev );
  202.  
  203. signals:
  204.     void valueChanged(double);
  205.  
  206. private slots:
  207.     void slotActivated( int );
  208.  
  209. protected:
  210.     double m_value;
  211.     double m_lower;
  212.     double m_upper;
  213.     double m_lowerInPoints; ///< lowest value in points
  214.     double m_upperInPoints; ///< highest value in points
  215. };
  216.  
  217. /**
  218.  * Combo box (with spin control) for double precision numbers with unit display
  219.  * \since 1.4 (change of behavior)
  220.  */
  221. class KOFFICEUI_EXPORT KoUnitDoubleSpinComboBox : public QWidget
  222. {
  223.     Q_OBJECT
  224. public:
  225.     KoUnitDoubleSpinComboBox( QWidget *parent = 0L, const char *name = 0L );
  226.     KoUnitDoubleSpinComboBox( QWidget *parent, double lower, double upper, double step, double value = 0.0, KoUnit::Unit unit = KoUnit::U_PT, unsigned int precision = 2, const char *name = 0 );
  227.  
  228.     void insertItem( double, int index = -1 );
  229.     void updateValue( double );
  230.     /// @return the current value, converted in points
  231.     double value( void ) const;
  232.  
  233. signals:
  234.     void valueChanged(double);
  235.  
  236. private slots:
  237.     void slotUpClicked();
  238.     void slotDownClicked();
  239.  
  240. private:
  241.     KoUnitDoubleComboBox *m_combo;
  242.     double m_step;
  243. };
  244.  
  245. #endif
  246.  
  247.